package com.ansion.basedb.util;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.ansion.basedb.R;
import com.ansion.basedb.dao.LoginedUserMgr;
import com.ansion.basedb.dao.helper.UserHelper;
import com.ansion.basedb.dao.model.CurrentUser;
import com.ansion.basedb.dao.model.GroupModel;
import com.ansion.basedb.dao.model.UserModel;
import com.ansion.string.StringUtils;
import com.ansion.utils.ResourceUtils;

import java.util.Locale;
import java.util.Set;


/**
 * created by zhaoyuntao
 * on 27/07/2022
 * description:
 */
public class GroupModelNameUtils {
    public static final int MAX = 8;

    public static String getDisplayName(@NonNull GroupModel groupModel) {

        if (groupModel.isVoipGroup()) {
            return getGroupCallDisplayName(groupModel);
        }

        if (!TextUtils.isEmpty(groupModel.getGroupName())) {
            return groupModel.getGroupName();
        } else {
//            GroupHelper.getGroupInfoRequest(groupModel.getId());
        }

        if (TextUtils.isEmpty(groupModel.getDisplayName())) {

            StringBuffer buf = new StringBuffer();
            Set<Long> users = groupModel.getUserIdSet();
            //群组只有一个人时显示
            if (users == null || users.size() == 1) {
                return ResourceUtils.getString(R.string.group_chat);
            }
            int i = 0;
            CurrentUser currentUser = LoginedUserMgr.getCurrentLoginedUser();
            for (long id : users) {
                if (i == MAX) {
                    break;
                }
                UserModel userModel = UserHelper.getUserModel(id);
                if (userModel != null) {

                    if (currentUser != null && currentUser.getUserId() == userModel.getUserId()) {
                        buf.append(ResourceUtils.getString(R.string.baba_grpchat_me));
                    } else {
                        buf.append(UserModelNameUtils.getDisplayName(userModel));
                    }
                    i++;
                    if (i < MAX && i < users.size()) {
                        buf.append(", ");
                    }
                }
            }
            groupModel.setDisplayName(buf.toString());
            if (StringUtils.isContainsArabic(groupModel.getDisplayName())) {
                groupModel.setDisplayName(String.format(new Locale("ar"), groupModel.getDisplayName()));
            }
        }
        return groupModel.getDisplayName();

    }

    private static String getGroupCallDisplayName(@NonNull GroupModel groupModel) {


        CurrentUser currentUser = LoginedUserMgr.getCurrentLoginedUser();
        if (currentUser == null) {
            return ResourceUtils.getString(R.string.baba_grpcall);
        }

        groupModel.isNeedReCreate.set(true);//add by mawenqiang  通讯录或者昵称修改，也需要同步到session，不再缓存groupCallName
        //增加一个纠错的判断
        if (!TextUtils.isEmpty(groupModel.getGroupCallName())) {
            if (groupModel.isNeedReCreate.get()) {
                groupModel.setGroupCallName("");
            } else {
                String[] args = groupModel.getGroupCallName().split(", ");
                if (args == null || args.length != groupModel.getUserIdSet().size()) {
                    groupModel.setGroupCallName("");
                }
            }
        }

        if (TextUtils.isEmpty(groupModel.getGroupCallName())) {

            StringBuffer buf = new StringBuffer();
            Set<Long> users = groupModel.getUserIdSet();

            if (users == null || users.size() == 0) {
                return ResourceUtils.getString(R.string.baba_grpcall);
            }

            int i = 0;
            for (long id : users) {
                UserModel userModel = UserHelper.getUserModel(id);
                if (userModel != null) {

                    if (currentUser.getUserId() == userModel.getUserId()) {
                        buf.append(ResourceUtils.getString(R.string.baba_grpchat_me));
                    } else {
                        buf.append(UserModelNameUtils.getNotificationName(userModel, true));
                    }

                    i++;
                    if (i < users.size()) {
                        buf.append(", ");
                    }
                }
            }
            groupModel.setGroupCallName(buf.toString());
            if (StringUtils.isContainsArabic(groupModel.getGroupCallName())) {
                return String.format(new Locale("ar"), groupModel.getGroupCallName());
            }
        }
        return groupModel.getGroupCallName();
    }
}
